<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   银行卡管理
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('user_bank');

if(IS_POST)
{
	$bank 		= NoBadStr($_POST['bank']);
	$bankcard	= NoBadStr($_POST['bankcard']);
	$truename	= NoBadStr($_POST['truename']);
	$lasttruename= NoBadStr($_POST['lasttruename']);
	$lastbankcard= NoBadStr($_POST['lastbankcard']);
	
	!$bank && AjaxReturn(1,'银行不能为空');	
	!$bankcard || !preg_match('/^[\d]{15,20}$/',$bankcard) && AjaxReturn(1,'银行卡号填写有误');
	!$truename && AjaxReturn(1,'开户人姓名不能为空');
	
	//	绑定的最后一张银行卡
	$data = $db->getrow('SELECT * FROM '.$table.' WHERE uid = '.$_uid.' AND status = 1 ORDER BY id DESC');
	if($data)
	{
		!$lasttruename && AjaxReturn(1,'最近一次绑定的银行卡姓名不能为空');	
		!$lastbankcard || !preg_match('/^[\d]{15,20}$/',$lastbankcard) && AjaxReturn(1,'最近一次绑定的银行卡卡号填写有误');
		
		($lasttruename != $data['truename']) && AjaxReturn(1,'最近一次绑定的银行卡姓名不正确');
		($lastbankcard != $data['bankcard']) && AjaxReturn(1,'最近一次绑定的银行卡卡号不正确');
		
		($lasttruename != $truename) && AjaxReturn(1,'同一个账户下姓名必须相同');
	}
	
	$tmp = $db->getrow('SELECT 1 FROM '.$table.' WHERE bankcard = \''.$bankcard.'\'');
	$tmp && AjaxReturn(1,'该银行卡号已经被绑定了');
	
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['truename']=	$truename;
	$ins['bank']	=	$bank;
	$ins['bankcard']=	$bankcard;
	$ins['dateline']=	TIMESTAMP;
	$db->add('user_bank',$ins);
	
	AjaxReturn(0,'绑定银行卡成功！');
}

if($a == 'add')	//	邦新卡
{
	//	银行列表
	$banks = $db->getall('SELECT * FROM '.Table('web_bank').' WHERE status = 1 ORDER BY id');
	
	//	绑定的最后一张银行卡
	$data = $db->getrow('SELECT * FROM '.$table.' WHERE uid = '.$_uid.' AND status = 1 ORDER BY id DESC');

	$head['title'] = '新增银行卡';

	$tpl->display('member/bank_add');
	exit;
}

//	绑定的银行卡
$data = $db->getall('SELECT * FROM '.$table.' WHERE uid = '.$_uid.' AND status = 1 ORDER BY id DESC');

$head['title'] = '银行卡管理';

$tpl->display('member/bank');